package com.cec.service.admin.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cec.mapper.admin.UserMapper;
import com.cec.service.admin.UserService;
import com.cec.context.BaseContext;
import com.cec.dto.UserDTO;
import com.cec.dto.UserLoginDTO;
import com.cec.entity.Role;
import com.cec.entity.User;
import com.cec.exception.AccountException;
import com.cec.exception.OperationDataException;
import com.cec.result.Result;
import com.cec.result.ResultCode;
import com.cec.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author lt
 * @since 2024-10-25
 */
@Slf4j
@Service("adminService")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(UserLoginDTO userLoginDTO) {
        String username = userLoginDTO.getUserName();
        String password = userLoginDTO.getUserPwd();
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name", username);
        User user = userMapper.selectOne(wrapper);
        if (user == null)
            throw new AccountException(ResultCode.ACCOUNT_NOT_FOUND);
        if (user.getRole() != 2)
            throw new AccountException(ResultCode.USER_ERROR);
        if (!user.getUserPwd().equals(password))
            throw new AccountException(ResultCode.BAD_CREDENTIALS);
        return user;
    }

    @Override
    public Result pageQueryUserList(UserDTO userDTO) {
        //Page<User> page = ;
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (userDTO.getUserName() != null && !userDTO.getUserName().isEmpty())
            wrapper.like("u1.user_name", userDTO.getUserName());
        if (userDTO.getSex() != null)
            wrapper.eq("u1.sex", userDTO.getSex());
        if (userDTO.getRole() != null)
            wrapper.eq("u1.role", userDTO.getRole());
        wrapper.eq("u1.deleted", 0)
                .orderByDesc("u1.create_time");
        IPage<UserVO> userVOIPage = userMapper.queryPageUserList(new Page<>(userDTO.getPage(), userDTO.getPageSize()), wrapper);
        return Result.success(userVOIPage.getRecords(), userVOIPage.getTotal());
    }

    @Override
    public Result getRole() {
        List<Role> role = userMapper.getRole();
        return Result.success(role);
    }

    @Override
    public Result saveUser(UserDTO userDTO) {
        User user = new User();
        BeanUtils.copyProperties(userDTO, user);
        Integer currentId = BaseContext.getCurrentId();
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name", userDTO.getUserName());
        User user1 = baseMapper.selectOne(wrapper);
        if (user1 != null)
            throw new AccountException(ResultCode.USER_PRESENCE);
        user.setCreateUser(currentId);
        user.setUpdateUser(currentId);
        int insert = baseMapper.insert(user);
        if (insert > 0)
            return Result.success();
        throw new OperationDataException(ResultCode.OPERATION_DATA_ERROR);
    }

    @Override
    public void updateUser(UserDTO userDTO) {
        User user = new User();
        BeanUtils.copyProperties(userDTO, user);
        user.setUpdateUser(BaseContext.getCurrentId());
        int i = baseMapper.updateById(user);
        if (i <= 0)
            throw new OperationDataException(ResultCode.OPERATION_DATA_ERROR);
    }

    @Override
    public User queryUserById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public Result logicDeleteUserById(Integer id) {
        int i = baseMapper.deleteById(id);
        if (i > 0)
            return Result.success();
        throw new OperationDataException(ResultCode.OPERATION_DATA_ERROR);
    }

    @Override
    public Result logicBatchDeleteUserByIds(List<Integer> ids) {
        int i = baseMapper.deleteBatchIds(ids);
        if (i > 0)
            return Result.success();
        throw new OperationDataException(ResultCode.OPERATION_DATA_ERROR);
    }


}
